<template>
	<view class="container">
		<view v-for="l in logisticsList" style="border:1px solid rgba(225,164,143,0.64);margin-top: 10px  ">
			<view class="section">
				<view class="title">物流单号：{{l.logisticsNumber}}</view>
			</view>
			<view class="section">
				<view class="title">收件人信息：</view>
				<view class="content">姓名：{{l.receiverName}}</view>
				<view class="content">电话：<span style="color: #d39e00"><a style="text-decoration: none;"
							href="tel:l.receiverPhone">{{l.receiverPhone }}</a></span></view>
				<view class="content">地址：{{l.receiverAddress }}</view>
			</view>
			<button v-if="l.status==='3'" @click="sendOrder(l)"
				style="background-color: rgba(255,68,13,0.18)">开始送件</button>
			<view style="background-color: #f3914b" v-else-if="l.status==='4'">
				<button @click="sendOrder(l)" style="background-color:rgba(0,128,0,0.38)">{{info}}送件中.....</button>
				<button style="background-color: rgba(243,195,75,0.13)" @click="sureREach(l)">确认送达</button>
			</view>

			<button v-else-if="l.status>='5'" @click="" style="background-color:rgba(0,0,255,0.23)">已送达</button>
		</view>
	</view>
</template>
<script>
	import {
		findMyOrderById,
		findOrderById,
		findUserById,
		updateStatus,
		findLogisticsById
	} from "../../../api/logistics/logistics";

	export default {
		data() {
			return {
				longitude: 0,
				latitude: 0,
				postmanId: 0,
				logisticsList: [],
				polyline: [{
					points: [],
					color: "#00ff00",
					width: 10,

					borderWidth: 15 //线的边框宽度，还有很多参数，请看文档
				}],
				info: '',
				deviceId: '',

			}
		},
		async created() {
			// 获取快递单
			this.postmanId = this.$store.state.user.userId
			let logisticsList = await findMyOrderById({
				postmanId: this.postmanId
			})
			console.info(logisticsList)
			let i = 0;
			logisticsList.rows.forEach(l => {
				findOrderById(l.orderId).then(orderInfo => {
					console.info(l)
					findUserById({
						userId: orderInfo.data.userId
					}).then(user => {
						console.info(l.infoList)
						this.logisticsList.push({
							id: i++,
							logisticsId: l.id,
							logisticsNumber: l.logsNumber,
							receiverName: user.rows[0].userName,
							receiverPhone: user.rows[0].phonenumber,
							receiverAddress: l.address,
							status: l.status,
						})
						console.info(this.logisticsList)
					})
				})

			})
		},
		methods: {
			formatDate() {
				const date = new Date();
				const formattedDate =
					`${date.getFullYear()}-${this.padZero(date.getMonth() + 1)}-${this.padZero(date.getDate())} ${this.padZero(date.getHours())}:${this.padZero(date.getMinutes())}`;
				return formattedDate;
			},
			padZero(value) {
				return value.toString().padStart(2, "0");
			},
			updateLogistics(logistics) {
				findLogisticsById(logistics.logisticsNumber).then(res => {
					console.info(res)
					let infoList = res.data.infoList
					infoList.push({
						logisticsId: logistics.logisticsId,
						logsNumber: logistics.logisticsNumber,
						changeTime: this.formatDate(),
						currentAddress: infoList[0].currentAddress,
						status: logistics.status
					})
					console.info(infoList)
					updateStatus({
						id: logistics.logisticsNumber,
						status: logistics.status,
						infoList: infoList
					})
				})
			},
			sureREach(logistics) {
				logistics.status = '5'
				this.updateLogistics(logistics)

			},
			sendOrder(logistics) {
				logistics.status = '4'
				this.updateLogistics(logistics)

				uni.getLocation({
					type: 'gcj02',
					success: function(res) {
						console.log('当前位置的经度：' + res.longitude);
						console.log('当前位置的纬度：' + res.latitude);
					}
				});
				uni.getSystemInfo({
					success: (res) => {
						console.log(res.deviceId); //获取设备的唯一标识符
						this.deviceId = res.deviceId

						plus.geolocation.watchPosition(({
								coords
							}) => {
								let res = coords
								console.info(res)
								let l = this.polyline[0].points.length;
								console.info(l)
								if (l == 0) {
									this.polyline[0].points.push({
										latitude: res.latitude,
										longitude: res.longitude,
									})
									return
								}
								console.info(this.polyline[0].points[l - 1].latitude !== res.latitude ||
									this.polyline[0].points[l - 1].longitude !== res.longitude)
								if (this.polyline[0].points[l - 1].latitude !== res.latitude ||
									this.polyline[0].points[l - 1].longitude !== res.longitude) {
									console.info(2222222222)
									this.polyline[0].points.push({
										latitude: res.latitude,
										longitude: res.longitude,
									})
									res.date = new Date().getTime()

									const username = 'elastic';
									const password = '123456';
									const credentials = `${username}:${password}`;
									const encodedCredentials = btoa(credentials);
									console.info(res)
									uni.request({
										url: `http://124.70.51.123:9200/${this.deviceId.toLowerCase()}/_doc`,
										header: {
											'Authorization': `Basic ${encodedCredentials}`
										},
										data: res,
										method: "POST"
									}).then(o => {
										console.info(o);
										this.info += JSON.stringify(o);
									}).catch(error => {
										console.info(error)
									})
									this.polyline[0].points.push({
										latitude: res.latitude,
										longitude: res.longitude,
									})
									console.info(this.polyline[0].points)
								}

							},
							(e) => {
								uni.showToast({
									title: e.message,
									icon: 'none'
								});
							}, {
								provider: 'amap',
								//是否使用高精度设备，如GPS。默认是true
								enableHighAccuracy: true,
								//超时时间，单位毫秒，默认为0
								//使用设置时间内的缓存数据，单位毫秒
								//默认为0，即始终请求新数据
								//如设为Infinity，则始终使用缓存数据
								maximumAge: 3000
							}
						);
					}
				})
			}
		}
	}
</script>
<style>
	.container {
		width: 96%;
		margin: 10rpx auto;
		padding: 20rpx;
	}

	.section {
		margin-bottom: 20rpx;
	}

	.title {
		font-size: 28rpx;
		font-weight: bold;
	}

	.content {
		font-size: 36rpx;
		margin-top: 10rpx;
	}
</style>